package mspbots.xero.client.core;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;
import mspbots.xero.client.core.client.XeroClient;
import mspbots.xero.client.oauth.AuthService;
import mspbots.xero.client.oauth.TokenState;
import mspbots.xero.common.RabbitKeys;
import mspbots.xero.common.ResultMsg;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author billb
 */
@Log4j2
public abstract class AbstractToolsUtil {

    protected ObjectMapper objectMapper;

    protected XeroClient xeroClient;
    protected AuthService authService;

    protected RabbitMessagingTemplate messagingTemplate;
    protected ReactiveStringRedisTemplate stringRedisTemplate;
    protected ReactiveRedisTemplate<String, Object> reactiveRedisTemplate;

    private RabbitMessagingTemplate rabbitMessagingTemplate;

    protected Mono<TokenState> loadToken(Long tenantId) {
        return this.authService.getToken(TokenState.withDefault(tenantId));
    }

    /**
     * default sync all business data, such as "ticket","timeEntry"
     */
    protected Mono<LocalDateTime> readAnchorTime(String key) {
        return this.stringRedisTemplate.opsForValue().get(key)
                .map(LocalDateTime::parse)
                .defaultIfEmpty(LocalDateTime.now().minusYears(20));
    }

    protected void recordAnchorTime(String key, String value) {
        LocalDateTime nowAnchorTime = LocalDateTime.parse(value, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        this.readAnchorTime(key)
                .filter(v -> v.isBefore(nowAnchorTime))
                .flatMap(v -> this.stringRedisTemplate.opsForValue()
                        .set(key, nowAnchorTime.format(DateTimeFormatter.ISO_DATE_TIME), Duration.ofDays(30)))
                .subscribe();
    }

    protected void sendEntity(String key, ResultMsg resultMsg) {
        this.rabbitMessagingTemplate.convertAndSend(RabbitKeys.BASE_EXCHANGE_NAME, key, resultMsg);
    }

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setMessagingTemplate(RabbitMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    @Autowired
    public void setStringRedisTemplate(ReactiveStringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Autowired
    public void setReactiveRedisTemplate(ReactiveRedisTemplate<String, Object> redisTemplate) {
        this.reactiveRedisTemplate = redisTemplate;
    }

    @Autowired
    public void setRabbitMessagingTemplate(RabbitMessagingTemplate rabbitMessagingTemplate) {
        this.rabbitMessagingTemplate = rabbitMessagingTemplate;
    }

    @Autowired
    public void setXeroClient(XeroClient xeroClient) {
        this.xeroClient = xeroClient;
    }

    @Autowired
    public void setAuthService(AuthService authService) {
        this.authService = authService;
    }
}
